package com.icss.mqsp.data;

import com.icss.mqsp.builder.IBuilder;
import com.icss.mqsp.data.build.IDataBuilder;
import com.icss.mqsp.data.build.factory.DataBuilderFactory;
import com.icss.mqsp.data.encrypt.IDataEncrypt;
import com.icss.mqsp.data.encrypt.impl.Base64Encrypt;
import com.icss.mqsp.data.process.IDataProcessor;
import com.icss.mqsp.data.process.factory.DataProcessorFactory;
import com.icss.mqsp.model.ReportDefine;
import com.icss.mqsp.quartz.util.CacheUtil;
import com.icss.mqsp.util.AnalyseJsonUtil;
import com.icss.mqsp.util.RequestCondition;
import com.icss.pangu.logging.Log;
import com.icss.pangu.logging.LogFactory;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;

public class ReportBuilder
  implements IBuilder
{
  private static Log log = LogFactory.getLog(ReportBuilder.class);
  
  public JSONObject exec(RequestCondition rc)
  {
    String pid = (String)rc.getCondition("pid");
    JSONObject object;
    Map<String, String> map;
    if ("INDEX".equals(pid))
    {
      List<Map<String, String>> array = new ArrayList();
      Map<String, String> map0 = new HashMap();
      map0.put("Y", "INDEX_XSFX_Y");
      map0.put("M", "INDEX_XSFX_M");
      map0.put("D", "INDEX_XSFX_D");
      
      Map<String, String> map1 = new HashMap();
      map1.put("Y", "INDEX_CWKB_Y");
      Calendar calendar = Calendar.getInstance();
      int count = calendar.get(2);
      for (int i = 1; i <= count; i++)
      {
        String str = "0" + calendar.get(2);
        map1.put(calendar.get(1) + str, "INDEX_CWKB_M");
        calendar.add(2, -1);
      }
      Map<String, String> map2 = new HashMap();
      


      Map<String, String> map3 = new HashMap();
      array.add(map0);
      array.add(map1);
      array.add(map2);
      array.add(map3);
      String params = rc.getPamams();
      log.info("params=" + params);
      
      Map<String, String> conditionMap = rc.getConditionMap();
      
      JSONObject allObject = new JSONObject();
      object = new JSONObject();
      int index = 0;
      for (Iterator localIterator1 = array.iterator(); localIterator1.hasNext();)
      {
        map = (Map)localIterator1.next();
        for (String key : map.keySet()) {
          if (StringUtils.isNumeric(key))
          {
            conditionMap.put("D_MONTH", key);
            JSONObject rs1 = buildPageDataNew((String)map.get(key), conditionMap, params);
            object.put(Integer.parseInt(key.substring(4)), rs1.get("value"));
          }
          else
          {
            JSONObject rs1 = buildPageDataNew((String)map.get(key), conditionMap, params);
            object.put(key, rs1.get("value"));
          }
        }
        allObject.put(index, object);
        object = new JSONObject();
        index++;
      }
      return allObject;
    }
    if ("CWZB_QG".equals(pid))
    {
      List<Map<String, String>> array = new ArrayList();
      
      Map<String, String> map1 = new HashMap();
      map1.put("Y", "CWKB_QG_Y");
      Calendar calendar = Calendar.getInstance();
      int count = calendar.get(2);
      for (int i = 1; i <= count; i++)
      {
        String str = "0" + calendar.get(2);
        map1.put(calendar.get(1) + str, "CWKB_QG_M");
        calendar.add(2, -1);
      }
      array.add(map1);
      String params = rc.getPamams();
      log.info("params=" + params);
      
      Map<String, String> conditionMap = rc.getConditionMap();
      
      JSONObject allObject = new JSONObject();
      JSONObject object1 = new JSONObject();
      int index = 0;
      for (Map<String, String> map11 : array)
      {
        for (String key : map11.keySet()) {
          if (StringUtils.isNumeric(key))
          {
            conditionMap.put("D_MONTH", key);
            JSONObject rs1 = buildPageDataNew((String)map11.get(key), conditionMap, params);
            object1.put(Integer.parseInt(key.substring(4)), rs1.get("value"));
          }
          else
          {
            JSONObject rs1 = buildPageDataNew((String)map11.get(key), conditionMap, params);
            object1.put(key, rs1.get("value"));
          }
        }
        allObject.put(index, object1);
        object1 = new JSONObject();
        index++;
      }
      return allObject;
    }
    String params = rc.getPamams();
    
    log.info("params=" + params);
    
    Map<String, String> conditionMap = rc.getConditionMap();
    if (conditionMap.containsKey("CON"))
    {
      String value = (String)conditionMap.get("CON");
      try
      {
        value = URLDecoder.decode(value, "UTF-8");
        value = URLDecoder.decode(value, "UTF-8");
      }
      catch (UnsupportedEncodingException e)
      {
        e.printStackTrace();
      }
      conditionMap.put("CON", value);
    }
    JSONObject rs = buildPageData(pid, conditionMap, params);
    
    return rs;
  }
  
  public JSONObject execNew(RequestCondition rc)
  {
    String pid = (String)rc.getCondition("pid");
    
    String params = rc.getPamams();
    
    log.info("params=" + params);
    
    Map<String, String> conditionMap = rc.getConditionMap();
    
    JSONObject rs = buildPageDataNew(pid, conditionMap, params);
    
    return rs;
  }
  
  public static JSONObject buildPageData(String pid, Map conditionMap, String params)
  {
    List<ReportDefine> reportDefineList = AnalyseJsonUtil.getDefine(pid);
    
    JSONObject reJson = new JSONObject();
    reJson.put("key", pid);
    reJson.put("params", params);
    reJson.put("pubTime", CacheUtil.updateTime);
    JSONArray jsonArray = new JSONArray();
    ReportDefine rd = null;
    String data = null;
    JSONObject jobj = null;
    for (int i = 0; i < reportDefineList.size(); i++)
    {
      rd = (ReportDefine)reportDefineList.get(i);
      
      data = buildData(rd, conditionMap);
      if (!rd.getDataSource().equals("SQL"))
      {
        jobj = processData(rd, data, conditionMap);
        
        jsonArray.add(i, jobj);
      }
      else
      {
        jsonArray = JSONArray.fromObject(data);
      }
    }
    log.info(jsonArray.toString());
    
    reJson.put("value", jsonArray.toString());
    
    return reJson;
  }
  
  public static JSONObject buildPageDataNew(String pid, Map conditionMap, String params)
  {
    List<ReportDefine> reportDefineList = AnalyseJsonUtil.getDefine(pid);
    
    JSONObject reJson = new JSONObject();
    reJson.put("key", pid);
    reJson.put("params", params);
    reJson.put("pubTime", CacheUtil.updateTime);
    JSONArray jsonArray = new JSONArray();
    ReportDefine rd = null;
    String data = null;
    JSONObject jobj = null;
    for (int i = 0; i < reportDefineList.size(); i++)
    {
      rd = (ReportDefine)reportDefineList.get(i);
      
      data = buildData(rd, conditionMap);
    }
    log.info(jsonArray.toString());
    
    System.out.println(data);
    reJson.put("value", data);
    
    return reJson;
  }
  
  public static String buildData(ReportDefine rd, Map<String, String> conditionMap)
  {
    log.info("datasource=" + rd.getDataSource());
    IDataBuilder dataBuilder = DataBuilderFactory.getDataBuilder(rd.getDataSource());
    String data = dataBuilder.getData(rd, conditionMap);
    return data;
  }
  
  public static String encrtyData(String data)
  {
    IDataEncrypt id1 = new Base64Encrypt();
    data = id1.encrtpy(data);
    


    return data;
  }
  
  public static JSONObject processData(ReportDefine rd, String data, Map<String, String> conditionMap)
  {
    IDataProcessor dataProcessor = DataProcessorFactory.getDataProcesser(rd.getBuildType());
    JSONObject jobj = dataProcessor.processData(rd, data, conditionMap);
    return jobj;
  }
}
